import 'package:flutter/material.dart';
import 'package:flutter_account_book/pages/analysis/logic.dart';
import 'package:flutter_account_book/pages/analysis/widgets/charts.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:omni_datetime_picker/omni_datetime_picker.dart';

class AnalysisPage extends StatefulWidget {
  const AnalysisPage({super.key});

  @override
  State<StatefulWidget> createState() => _AnalysisPage();
}

class _AnalysisPage extends State<AnalysisPage> with TickerProviderStateMixin {
  late TabController tabController = TabController(length: 2, vsync: this);

  final tabList = ["支出", "收入"];

  late DateTime firstTime;
  late DateTime lastTime;
  String get dateTimeStr {
    DateFormat dateFormat = DateFormat("yyyy-MM-dd");
    return "${dateFormat.format(firstTime)} - ${dateFormat.format(lastTime)}";
  }

  @override
  void initState() {
    DateTime now = DateTime.now();
    firstTime = DateTime(now.year, now.month, 1);
    lastTime =
        DateTime(now.year, now.month + 1, 1).subtract(const Duration(days: 1));
    Get.put(AnalysisLogic());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final curId = Get.arguments;
    return Scaffold(
      appBar: AppBar(
        title: Text('分析'),
      ),
      body: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextButton.icon(
                onPressed: () async {
                  final date = await showDateRangePicker(
                      context: context,
                      firstDate: DateTime(1970),
                      lastDate: DateTime.now().add(Duration(days: 1000)),
                      initialDateRange:
                          DateTimeRange(start: firstTime, end: lastTime),
                      initialEntryMode: DatePickerEntryMode.calendarOnly);
                  if (date != null) {
                    setState(() {
                      firstTime = date.start;
                      lastTime = date.end;
                    });
                  }
                },
                label: Text(dateTimeStr),
                icon: Icon(Icons.calendar_month),
              ),
            ],
          ),
          TabBar(
              controller: tabController,
              tabs: List.generate(
                  tabList.length,
                  (index) => Tab(
                        text: tabList[index],
                      ))),
          Expanded(
              child: Container(
            margin: EdgeInsets.fromLTRB(15, 8, 15, 0),
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(4))),
            child: TabBarView(controller: tabController, children: [
              ChartsPage(
                  curId: curId,
                  isIncome: false,
                  range: DateTimeRange(start: firstTime, end: lastTime)),
              ChartsPage(
                  curId: curId,
                  isIncome: true,
                  range: DateTimeRange(start: firstTime, end: lastTime))
            ]),
          ))
        ],
      ),
    );
  }
}
